/*
 * Created by Jaren at 2021/10/29 10:50
 */

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:inmotion_protocol/ezcan/bean/scooter/scooter_settings.dart';
import 'package:inmotion_protocol/ezcan/ezcan_constant.dart';
import 'package:inmotion_protocol_example/module/base/base.dart';
import 'package:inmotion_protocol_example/module/operation/operation_controller.dart';

class OperationPage extends BaseView<OperationController> {
  _buildActionViews(OperationController controller) {
    return Column(
      mainAxisSize: MainAxisSize.min,
      children: [
        Wrap(
          children: [
            MaterialButton(
              onPressed: () => {controller.requestProductProperty()},
              child: const Text('初始化'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestAllRealTimeInfo()},
              child: const Text('获取全部实时信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestRealTimeInfo()},
              child: const Text('获取实时信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestRealTimeState()},
              child: const Text('获取实时状态'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestRealTimeError()},
              child: const Text('获取实时故障'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestCurrentHistoryStatistics()},
              child: const Text('获取当前统计信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestTotalStatistics()},
              child: const Text('获取总统计信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestHistoryStatistics(0)},
              child: const Text('获取历史统计信息列表[0]'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestAccessories()},
              child: const Text('获取配件信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestBatteryRealTimeInfo()},
              child: const Text('获取电池实时信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestSettings()},
              child: const Text('获取设置数据'),
            ),
            MaterialButton(
              onPressed: () => {controller.buildSecurityConnection()},
              child: const Text('触发安全连接'),
            ),
            MaterialButton(
              onPressed: () => {controller.activate()},
              child: const Text('激活'),
            ),
            MaterialButton(
              onPressed: () => {controller.headlight(true)},
              child: const Text('前灯[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.headlight(false)},
              child: const Text('前灯[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.headlight2(true, false)},
              child: const Text('近光灯【开】'),
            ),
            MaterialButton(
              onPressed: () => {controller.headlight2(false, true)},
              child: const Text('远光灯【开】'),
            ),
            MaterialButton(
              onPressed: () => {controller.headlight2(true, true)},
              child: const Text('远近光灯【开】'),
            ),
            MaterialButton(
              onPressed: () => {controller.headlight2(false, false)},
              child: const Text('远近光灯【关】'),
            ),
            MaterialButton(
              onPressed: () => {controller.headlight(false)},
              child: const Text('前灯[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.lowBatterySafeMode(true)},
              child: const Text('低电安全[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.lowBatterySafeMode(false)},
              child: const Text('低电安全[关]'),
            ),
            MaterialButton(
              onPressed: () => {
                controller.headlightBrightness(
                    lowBeamBrightness: 40, highBeamBrightness: 70)
              },
              child: const Text('前灯亮度[40,70]'),
            ),
            MaterialButton(
              onPressed: () => {controller.lock(true)},
              child: const Text('锁车'),
            ),
            MaterialButton(
              onPressed: () => {controller.lock(false)},
              child: const Text('解锁'),
            ),
            MaterialButton(
              onPressed: () => {controller.gotoUpdateFw()},
              child: const Text('固件更新'),
            ),
            MaterialButton(
              onPressed: () => {controller.transportationMode(true)},
              child: const Text('运输模式[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.transportationMode(false)},
              child: const Text('运输模式[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.volume(10)},
              child: const Text('音量[10]'),
            ),
            MaterialButton(
              onPressed: () => {controller.volume(40)},
              child: const Text('音量[40]'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestAvatar()},
              child: const Text('获取头像'),
            ),
            MaterialButton(
              onPressed: () => {controller.uploadAvatar()},
              child: const Text('上传头像'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestLog()},
              child: const Text('获取日志'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestBMSLog()},
              child: const Text('获取BMS日志'),
            ),
            MaterialButton(
              onPressed: () => {controller.setPedalSensitivity(80, 60)},
              child: const Text('踏板灵敏度[80,60]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setStandbyTime(50 * 60)},
              child: const Text('待机时长[50min]'),
            ),
            MaterialButton(
              onPressed: () => {controller.horn()},
              child: const Text('鸣笛'),
            ),
            MaterialButton(
              onPressed: () => {controller.factoryReset(0)},
              child: const Text('恢复出厂设置[0]'),
            ),
            MaterialButton(
              onPressed: () => {controller.forceUpdate()},
              child: const Text('强制固件更新'),
            ),
            MaterialButton(
              onPressed: () => {controller.soundPackOnOff(true)},
              child: const Text('音效[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.soundPackOnOff(false)},
              child: const Text('音效[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.gotoUploadLightEffectPkg()},
              child: const Text('上传灯效包'),
            ),
            MaterialButton(
              onPressed: () => {controller.getLightEffectIdList()},
              child: const Text('获取灯效ID列表'),
            ),
            MaterialButton(
              onPressed: () => {controller.setLightEffectId(43703)},
              child: const Text('设置当前灯效包[43703]'),
            ),
            MaterialButton(
              onPressed: () => {controller.spinKillOnOff(true)},
              child: const Text('防飞转[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.spinKillOnOff(false)},
              child: const Text('防飞转[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.lightEffectOnOff(true)},
              child: const Text('灯效[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.lightEffectOnOff(false)},
              child: const Text('灯效[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.cruiseOnOff(true)},
              child: const Text('定速巡航[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.cruiseOnOff(false)},
              child: const Text('定速巡航[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.getAudioItemIndexList()},
              child: const Text('获取音效包列表'),
            ),
            MaterialButton(
              onPressed: () => {controller.driveMode(0)},
              child: const Text('驱动模式[Comfort]'),
            ),
            MaterialButton(
              onPressed: () => {controller.driveMode(1)},
              child: const Text('驱动模式[Classic]'),
            ),
            MaterialButton(
              onPressed: () => {controller.gotoUploadAudioPkg()},
              child: const Text('上传音效包'),
            ),
            MaterialButton(
              onPressed: () => {controller.speedLimit(20 * 100)},
              child: const Text('限速[20]'),
            ),
            MaterialButton(
              onPressed: () => {controller.speedLimit(5 * 100)},
              child: const Text('限速[5]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setPasswordType(1)},
              child: const Text('密码类型[1]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setPitchAngleZero(-3 * 100)},
              child: const Text('平衡角度[-5]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setPitchAngleZero(3 * 100)},
              child: const Text('平衡角度[6]'),
            ),
            MaterialButton(
              onPressed: () =>
                  {controller.setSpeedUnit(ScooterSettings.speedUnitKmph)},
              child: const Text('公制'),
            ),
            MaterialButton(
              onPressed: () =>
                  {controller.setSpeedUnit(ScooterSettings.speedUnitMph)},
              child: const Text('英制'),
            ),
            MaterialButton(
              onPressed: () => {controller.setFrontRearBrakeForce(30)},
              child: const Text('前后刹车力度[30]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setFrontRearBrakeForce(100)},
              child: const Text('前后刹车力度[100]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setRidingMode(0)},
              child: const Text('骑行模式[0]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setRidingMode(1)},
              child: const Text('骑行模式[1]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setRidingMode(2)},
              child: const Text('骑行模式[2]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setAssistantMode(0)},
              child: const Text('助力模式[0]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setAssistantMode(1)},
              child: const Text('助力模式[1]'),
            ),
            MaterialButton(
              onPressed: () => {controller.setAssistantMode(2)},
              child: const Text('助力模式[2]'),
            ),
            MaterialButton(
              onPressed: () => {controller.zeroSpeedStartupOnOff(true)},
              child: const Text('零速启动[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.zeroSpeedStartupOnOff(false)},
              child: const Text('零速启动[关]'),
            ),
            MaterialButton(
              onPressed: () => {
                controller.setLightEffectId(
                    L8Settings.lightEffectTailLightFlag, 0x00FF00)
              },
              child: const Text('尾灯颜色[0x00FF00]'),
            ),
            MaterialButton(
              onPressed: () => {controller.overSpeedWarningOnOff(true)},
              child: const Text('超速报警开关[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.overSpeedWarningOnOff(false)},
              child: const Text('超速报警开关[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.powerOnOff(true)},
              child: const Text('开机'),
            ),
            MaterialButton(
              onPressed: () => {controller.powerOnOff(false)},
              child: const Text('关机'),
            ),
            MaterialButton(
              onPressed: () => {controller.gpsOnOff(true)},
              child: const Text('GPS[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.gpsOnOff(false)},
              child: const Text('GPS[关]'),
            ),
            MaterialButton(
              onPressed: () =>
                  {controller.setLanguage(EzcanConstant.LANGUAGE_CHINESE)},
              child: const Text('语言[中]'),
            ),
            MaterialButton(
              onPressed: () =>
                  {controller.setLanguage(EzcanConstant.LANGUAGE_ENGLISH)},
              child: const Text('语言[英]'),
            ),
            MaterialButton(
              onPressed: () => {controller.turningSensitivity(20)},
              child: const Text('转向灵敏度[20]'),
            ),
            MaterialButton(
              onPressed: () => {controller.turningSensitivity(80)},
              child: const Text('转向灵敏度[80]'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestBMSFixedInfo()},
              child: const Text('获取BMS固定信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestBMSCellsVoltage()},
              child: const Text('获取BMS电芯电压'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestBMSRealTimeInfo()},
              child: const Text('获取BMS实时信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.bermAngleModeOnOff(true)},
              child: const Text('赛道倾斜角[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.bermAngleModeOnOff(false)},
              child: const Text('赛道倾斜角[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.assistBalanceOnOff(true)},
              child: const Text('辅助平衡[开]'),
            ),
            MaterialButton(
              onPressed: () => {controller.assistBalanceOnOff(false)},
              child: const Text('辅助平衡[关]'),
            ),
            MaterialButton(
              onPressed: () => {controller.calibrateAccHandle()},
              child: const Text('启动油门把手校准'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryHandleCalibrationResult(0)},
              child: const Text('查询油门把手校准结果'),
            ),
            MaterialButton(
              onPressed: () => {controller.calibrateBrakeHandle()},
              child: const Text('启动刹车把手校准'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryHandleCalibrationResult(1)},
              child: const Text('查询刹车把手校准结果'),
            ),
            MaterialButton(
              onPressed: () => {controller},
              child: const Text('启动滑板车IMU校准'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryHandleCalibrationResult(2)},
              child: const Text('查询滑板车IMU校准结果'),
            ),
            MaterialButton(
              onPressed: () => {controller.startTurningCalibration()},
              child: const Text('启动转弯前倾校准'),
            ),
            MaterialButton(
              onPressed: () => {controller.startBalanceCalibration()},
              child: const Text('启动平衡角度校准'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryTurningCalibrationResult()},
              child: const Text('查询转弯前倾校准结果'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryBalanceCalibrationResult()},
              child: const Text('查询平衡角度校准结果'),
            ),
            MaterialButton(
              onPressed: () => {controller.startMotorAutoCompat()},
              child: const Text('启动电机自适应'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryMotorAutoCompatResult()},
              child: const Text('查询电机自适应结果'),
            ),
            MaterialButton(
              onPressed: () => {controller.startMotorSelfCheck()},
              child: const Text('启动电机自检'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryMotorSelfCheckResult()},
              child: const Text('查询电机自检结果'),
            ),
            MaterialButton(
              onPressed: () => {controller.startBatteryCalibration()},
              child: const Text('启动电池校准'),
            ),
            MaterialButton(
              onPressed: () => {controller.queryBatteryCalibrationResult()},
              child: const Text('查询电池校准结果'),
            ),
            MaterialButton(
              onPressed: () => {controller.getEsimList()},
              child: const Text('查询云盒ESIM信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.requestTBoxRealTimeInfo()},
              child: const Text('查询云盒实时信息'),
            ),
            MaterialButton(
              onPressed: () => {controller.showTboxInfo(true)},
              child: const Text('设置云盒信息【显示】'),
            ),
            MaterialButton(
              onPressed: () => {controller.showTboxInfo(false)},
              child: const Text('设置云盒信息【隐藏】'),
            ),
            MaterialButton(
              onPressed: () => {controller.lowBatteryWakeUp(true)},
              child: const Text('设置云盒低电唤醒【开启】'),
            ),
            MaterialButton(
              onPressed: () => {controller.lowBatteryWakeUp(false)},
              child: const Text('设置云盒低电唤醒【关闭】'),
            ),
            MaterialButton(
              onPressed: () => {controller.safeSpeedLimitOnOff(true)},
              child: const Text('打开安全限速'),
            ),
            MaterialButton(
              onPressed: () => {controller.safeSpeedLimitOnOff(false)},
              child: const Text('关闭安全限速'),
            ),
          ],
        ),
      ],
    );
  }

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return WillPopScope(
        child: Scaffold(
            appBar: AppBar(
              title: const Text("Operation"),
            ),
            body: Column(
              children: [
                Expanded(
                  child: SingleChildScrollView(
                    child: Column(
                      mainAxisSize: MainAxisSize.min,
                      children: [
                        _buildActionViews(controller),
                      ],
                    ),
                  ),
                ),
              ],
            )),
        onWillPop: () => _onBackPressed(controller));
  }

  Future<bool> _onBackPressed(OperationController controller) {
    return controller.onPageClose();
  }
}
